package tu.java.movie;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import tu.java.db.DBConnManager;

public class Genre {
	
	
	
	private int		genreId;
	private String	genreName;
	
	
	
	public static Vector<Genre> searchGenreForMovie(int movieId) {
		Vector<Genre> genres = new Vector<Genre>();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			StringBuilder sql = new StringBuilder();
			sql.append(" SELECT CM02_MOVIE_GENRE_ID,CS01_DESCRIPTION FROM TM02_MOVIE_GENRES,TS01_NOMENCLATURES");
			sql.append(" WHERE CS01_NOMENCLATURE_ID=CS01_GENRE_ID AND CM01_MOVIE_ID=?");
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setInt(1, movieId);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				Genre g = new Genre();
				g.genreId = rs.getInt(1);
				g.genreName = rs.getString(2);
				genres.add(g);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(rs);
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return genres;
	}
	
	
	
	public static boolean addGenre(int genreId, int movieId) {
		boolean status = true;
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			StringBuilder sql = new StringBuilder();
			sql.append(" INSERT INTO TM02_MOVIE_GENRES (CS01_GENRE_ID,CM01_MOVIE_ID)");
			sql.append(" VALUES (?,?)");
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setInt(1, genreId);
			pstmt.setInt(2, movieId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	
	public static boolean deleteGenre(int genreId, int movieId) {
		boolean status = true;
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn = DBConnManager.getInstance().getConnection();
			StringBuilder sql = new StringBuilder();
			sql.append(" DELETE FROM TM02_MOVIE_GENRES");
			sql.append(" WHERE CS01_GENRE_ID=? AND CM01_MOVIE_ID=?");
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setInt(1, genreId);
			pstmt.setInt(2, movieId);
			pstmt.executeUpdate();
		}
		catch (Exception e) {
			status = false;
			e.printStackTrace();
		}
		finally {
			DBConnManager.getInstance().close(pstmt);
			DBConnManager.getInstance().freeConnection(conn);
		}
		return status;
	}
	
	
	
	public int getGenreId() {
		return genreId;
	}
	
	
	
	public void setGenreId(int genreId) {
		this.genreId = genreId;
	}
	
	
	
	public String getGenreName() {
		return genreName;
	}
	
	
	
	public void setGenreName(String genreName) {
		this.genreName = genreName;
	}
}
